package com.spx.core.handlers;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import com.spx.core.types.User;
import com.spx.kernel.Application;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class CopyUserHandler extends AbstractHandler
{
	/**
	 * The constructor.
	 */
	public CopyUserHandler()
	{
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException
	{

		final IStructuredSelection selection = getSelection(event);

		final Collection<User> selected = new ArrayList<User>();

		for (final Object object : selection.toArray())
		{
			if (object instanceof User)
			{
				selected.add((User) object);
			}
		}
		Application.setClipboard(selected);
		return null;
	}

	private IStructuredSelection getSelection(ExecutionEvent event)
	{
		final IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
		return selection;
	}

}
